﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using ShortcutToolbox.Core;

namespace ShortcutToolbox.View
{
    partial class LogForm : Form, ILangConnector
    {
        static LogForm _instance = null;
        static object _locker = new object();

        public static LogForm Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_locker)
                    {
                        if (_instance == null)
                            _instance = new LogForm();
                    }
                }
                return _instance;
            }
        }

        public LogForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.Manual;
            this.Left = GetBestLeft();
            this.Top = GetBestTop();
            topMostToolStripMenuItem.Checked = this.TopMost;
            I18N.Changed += OnLangeChanged;
            OnLangeChanged(this, new LanguageChangedEventArgs(I18N.Current));
        }

        public void OnLangeChanged(object sender, LanguageChangedEventArgs e)
        {
            dynamic dylang = e.Value;
            Text = dylang.LogForm.Title;
            fileToolStripMenuItem.Text = dylang.LogForm.Menu.File;
            viewToolStripMenuItem.Text = dylang.LogForm.Menu.View;
            openLocationToolStripMenuItem.Text = dylang.LogForm.Menu.OpenLocation;
            cleanLogsToolStripMenuItem.Text = dylang.LogForm.Menu.CleanLogs;
            wrapTextToolStripMenuItem.Text = dylang.LogForm.Menu.WrapText;
            changeFontToolStripMenuItem.Text = dylang.LogForm.Menu.ChangeFont;
            resetFontToolStripMenuItem.Text = dylang.LogForm.Menu.ResetFont;
            topMostToolStripMenuItem.Text = dylang.LogForm.Menu.TopMost;
            exitToolStripMenuItem.Text = dylang.LogForm.Menu.Exit;
        }

        protected override void OnClosed(EventArgs e)
        {
            I18N.Changed -= OnLangeChanged;
            base.OnClosed(e);
            _instance = null;
        }

        private void LogForm_Load(object sender, EventArgs e)
        {
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            logViewerUserControl1.ScrollToCaret();
        }

        public int GetBestLeft()
        {
            return Screen.PrimaryScreen.WorkingArea.Width - this.Width;
        }

        public int GetBestTop()
        {
            return Screen.PrimaryScreen.WorkingArea.Height - this.Height;
        }

        private void openLocationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string argument = "/select, \"" + Logging.LogFilePath + "\"";
            Process.Start("explorer.exe", argument);
        }

        private void cleanLogsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            logViewerUserControl1.DoCleanLogs();
        }

        private void wrapTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            wrapTextToolStripMenuItem.Checked = logViewerUserControl1.TriggerWrapText();
        }

        private void changeFontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            logViewerUserControl1.DoChangeFont();
        }

        private void resetFontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            logViewerUserControl1.ResetViewerFont();
        }

        private void topMostToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TopMost = topMostToolStripMenuItem.Checked = !topMostToolStripMenuItem.Checked;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
